package eteam.aps.service.impl;

import cn.hutool.core.bean.BeanUtil;
import eteam.aps.client.CommonClient;
import eteam.aps.common.component.LoginUserRedisService;
import eteam.aps.common.entity.ParentEntity;
import eteam.aps.common.helper.Helper;
import eteam.aps.common.helper.JsonPage;
import eteam.aps.mapper.WorkshopSectionMapper;
import eteam.aps.model.dto.WorkshopSectionQueryDto;
import eteam.aps.model.entity.WorkshopSection;
import eteam.aps.model.vo.workshop_section.WorkshopSectionShowVo;
import eteam.aps.model.vo.workshop_section.WorkshopSectionUpdateVo;
import eteam.aps.model.vo.workshop_section.WorkshopSectionVo;
import eteam.aps.service.WorkshopSectionService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 工段信息 业务层 实现类;
 *
 * @author LiuLei
 */
@Service
public class WorkshopSectionServiceImpl implements WorkshopSectionService {

    @Autowired
    private WorkshopSectionMapper workshopSectionMapper;

    @Autowired
    private LoginUserRedisService loginUserRedisService;

    @Autowired
    private CommonClient commonClient;

    @Override
    public JsonPage<WorkshopSectionShowVo> selWSJsp(WorkshopSectionQueryDto wsq) throws Exception {
        Map<String, Object> map = new HashMap<>(4);
        map.put("fState", ParentEntity.FSTATE_DELETED);
        map.put("offSize", wsq.offSize());
        map.put("size", wsq.getSize());
        if (Helper.isNotEmpty(wsq.getfName())) map.put("fName", Helper.likeSql(wsq.getfName()));
        int count = workshopSectionMapper.selCountByWsq(map);
        if (count == 0) return JsonPage.builder(wsq.getPage(), wsq.getSize(), count, Collections.emptyList());
        map.put("sort", "FCode");
        List<WorkshopSectionShowVo> list = workshopSectionMapper.selWSJsp(map);
        Map<String, String> usMap = commonClient.selectUserName();
        list.forEach(data -> {
            data.setfCreateUserId(usMap.get(data.getfCreateUserId()));
            data.setfLastModifyUserId(usMap.get(data.getfLastModifyUserId()));
        });
        return JsonPage.builder(wsq.getPage(), wsq.getSize(), count, list);
    }

    @Override
    @Transactional
    public int saveWS(WorkshopSectionUpdateVo wsu) throws Exception {
        WorkshopSection workshopSection = new WorkshopSection();
        BeanUtils.copyProperties(wsu, workshopSection);
        Helper.setDefaultAddValue(workshopSection, loginUserRedisService.getUser().getFuserid());
        return workshopSectionMapper.saveWS(workshopSection);
    }

    @Override
    @Transactional
    public int delWS(String fId) throws Exception {
        Map<String, Object> map = new HashMap<>(4);
        map.put("fState", ParentEntity.FSTATE_DELETED);
        map.put("fId", fId);
        map.put("fLastModifyTime", LocalDateTime.now());
        map.put("fLastModifyUserId", loginUserRedisService.getUser().getFuserid());
        return workshopSectionMapper.delWS(map);
    }

    @Override
    @Transactional
    public int updateWS(WorkshopSectionUpdateVo wsu) throws Exception {
        Helper.setEmptyWithNull(wsu);
        WorkshopSection workshopSection = BeanUtil.copyProperties(wsu, WorkshopSection.class);
        workshopSection.setfLastModifyTime(LocalDateTime.now());
        workshopSection.setfLastModifyUserId(loginUserRedisService.getUser().getFuserid());
        return workshopSectionMapper.updateWS(workshopSection);
    }

    @Override
    public JsonPage<WorkshopSectionVo> selWSSimJsp(WorkshopSectionQueryDto wsq) {
        Map<String, Object> map = new HashMap<>(5);
        map.put("fState", ParentEntity.FSTATE_DELETED);
        map.put("offSize", wsq.offSize());
        map.put("size", wsq.getSize());
        if (Helper.isNotEmpty(wsq.getfName())) map.put("fName", Helper.likeSql(wsq.getfName()));
        int count = workshopSectionMapper.selCountByWsq(map);
        if (count == 0) return JsonPage.builder(wsq.getPage(), wsq.getSize(), count, Collections.emptyList());
        map.put("sort", "FCode");
        return JsonPage.builder(wsq.getPage(), wsq.getSize(), count, workshopSectionMapper.selWSSimJsp(map));
    }

    @Override
    public int selByNameAndCode(String fCode, String fName) {
        Map<String, Object> map = new HashMap<>(3);
        map.put("fState", ParentEntity.FSTATE_DELETED);
        map.put("fName", fName);
        map.put("fCode", fCode);
        return workshopSectionMapper.selByNameAndCode(map);
    }
}